<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>物料类型编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/basic/syenum/materlist.do"></script>
<script type="text/javascript">
var basic_Url="${ctx}/basic/materbom";
var materGrid;
var process;
$(function(){
	if('${flag}'=='add'){
		$("#zd008MaterTypeName").val("${matertype.zd002Name}");
		$("#zd008MaterTypeId").val("${matertype.id}");
	}
	//产品系列选择方法
	$("#zd007ProductRange").change(function(){ 
		var ProductRange = $(this).val();
		multipleSearch("#table1",true,"",basic_Url+"/materList.do?ID=XL"+ProductRange);
		multipleSearch("#table2",true,"",basic_Url+"/processList.do?ID=XL"+ProductRange);
	});	
$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
            
        //alert(jsonInfo); 
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
            getReturnMessage(url,"inputForm",W.searchAP);	
        }catch(e){alert(e);}
    }
 });
 //原材料列表
materGrid=$("#table1").ligerGrid({
    columns: [
				{ display: '原材料ID', name: 'zd003MaterialsId', hide:true },
				{ display: '原材料类型ID', name: 'zd004MaterTypeId', hide:true },
                { display: '<font color="gray">原材料编码', name: 'zd001Code', width: 120,
              	  render: function (item){
            		  if(item.basicMaterialsDO==null){
            			  return item.zd001Code;
            		  }else{
            			  return item.basicMaterialsDO.zd001Code;
            		  }
            	  }},
                  { display: '<font color="gray">原材料名称</font>', name: 'zd002Name', width: 120,
                	  render: function (item){
                		  if(item.basicMaterialsDO==null){
                			  return item.zd002Name;
                		  }else{
                			  return item.basicMaterialsDO.zd002Name;
                		  }
                	  }},
                  { display: '<font color="gray">型号</font>', name: 'zd004Model', width: 120,
                    	  render: function (item){
                    		  if(item.basicMaterialsDO==null){
                    			  return item.zd004Model;
                    		  }else{
                    			  return item.basicMaterialsDO.zd004Model;
                    		  }
                 }},
                { display: '<font color="gray">尺寸</font>', name: 'zd005Size', width: 120,
                    	  render: function (item){
                    		  if(item.basicMaterialsDO==null){
                    			  return item.zd005Size;
                    		  }else{
                    			  return item.basicMaterialsDO.zd026Size;
                    		  }
                 }},
                { display: '<font color="gray">单位</font>', name: 'zd007Unit', width: 80,
                        	  render: function (item){
                        		  if(item.basicMaterialsDO==null){
                        			  return item.zd007Unit;
                        		  }else{
                        			  return item.basicMaterialsDO.basicStapleUnitDO.zd002Name;
                        		  }
                        	  }},
               { display: '工序名称', name: 'zd002ProcessName', width: 100,render: function (item){
         		  if(item.basicProcessDO==null){
        			  return item.zd002ProcessName;
        		  }else{
        			  return item.basicProcessDO.zd002Name;
        		  }
        	  	}},
                { display: '工序ID', name: 'zd002ProcessId',hide:true},
                { display: '材料状态', name: 'zd011UseMaterialsType', width: 100,editor: { type: 'select', data: ItemMLXList, valueColumnName: 'zd001Code', displayColumnName: 'zd002Name'}, render: function (item)
        	        {
        	        for (var i = 0; i < ItemMLXList.length; i++)
        	        {
        	            if (ItemMLXList[i]['zd001Code'] == item.zd011UseMaterialsType){
        	          	  return ItemMLXList[i]['zd002Name'];
        	            }
        	        }
        	        return item.zd011UseMaterialsType;
				}},
                { display: '用量<font color="red">*</font>', name: 'zd005Dosage', width: 100,editor : {type :'float'}},
                 { display: '备注', name: 'zd016Remark', width: 180,editor : {type :'text'}}
                  ],  usePager: true,pageSize:1000,pageSizeOptions :[1000],enabledEdit : true,
                  url : basic_Url+"/materList.do?ID=${materbom.id}",enabledSort:false,
                  width: "99%", height:200, checkbox: true, 
                  toolbar: { items: [
									  { text: '选择原材料', click: selectYCLItem, icon: 'add' },
                                      { line: true },
                                      { text: '工序', click: selectMTGXItem, icon: 'add' },
                                      { line: true },
                                      { text: '删除', click: deleteMaterRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                      ]
                                      }
              });
 //工序列表
process=$("#table2").ligerGrid({
    columns: [
              		{ display: '工序ID', name: 'zd002ProcessId',hide:true},
                  { display: '工序名称', name: 'zd002Name', width: 120,
                  	  render: function (item){
                		  if(item.basicProcessDO==null){
                			  return item.zd002Name;
                		  }else{
                			  return item.basicProcessDO.zd002Name;
                		  }
                	  }},
                  { display: '工序编码', name: 'zd001Code', width: 120,
                    	  render: function (item){
                    		  if(item.basicProcessDO==null){
                    			  return item.zd001Code;
                    		  }else{
                    			  return item.basicProcessDO.zd001Code;
                    		  }
                    	  }},
                  { display: '工序车间', name: 'zd001Name', width: 120,
                        	  render: function (item){
                        		  if(item.basicProcessDO==null){
                        			  return item.zd001Name;
                        		  }else{
                        			  return item.basicProcessDO.basicWorkshopDO.zd002Name;
                        		  }
                        	  }},
                  { display: '单件工时', name: 'zd006TaktTime', width: 120,editor : {type :'float'}},
                  { display: '委外面积', name: 'zd007OutsourcingArea', width: 120,editor : {type :'float'}},
                  { display: '尾料重量', name: 'zd008TailWeight', width: 120,editor : {type :'float'}},
                  { display: '委外单价', name: 'zd009OutsourcingPrice', width: 120,editor : {type :'float'}},
                  { display: '加工单价<font color="red">*</font>', name: 'zd010ProcessingPrice', width: 120,editor : {type :'float'}}	  
                  ],  usePager: true,pageSize:1000,pageSizeOptions :[1000],enabledEdit : true,
                  url : basic_Url+"/processList.do?ID=${materbom.id}",enabledSort:false,
                  width: "99%", height:200, checkbox: true, 
                  toolbar: { items: [
									  { text: '增加工序', click: selectGXItem, icon: 'add' },
                                      { line: true },
                                      { text: '删除', click: deleteProcessRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                      ]
                                      }
              });  
});

function deleteMaterRow(){
	materGrid.deleteSelectedRow();
}

function deleteProcessRow(){
	process.deleteSelectedRow();
}

function submitForm(param){
	//工序
	var data = $("#table2").ligerGetGridManager().getData();
	var datadel = $("#table2").ligerGetGridManager().getDeleted();
	//物料
	var data1 = $("#table1").ligerGetGridManager().getData();
	var datadel1 = $("#table1").ligerGetGridManager().getDeleted();
	if(data.length==0){
		$.dialog.alert("请增加工序信息");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		for (var i = 0;i < data.length;i++){
			var row = data[i];
			if (isNaN(row.zd010ProcessingPrice)||undefined == row.zd010ProcessingPrice
					||null == row.zd010ProcessingPrice||row.zd010ProcessingPrice==""
					||row.zd010ProcessingPrice<=0) {
				$.dialog.alert("第" + (i+1) + "行的单价没有填写,或者填写错误(单价不能为0)");
				ba = false;
				return false;
			}
			
		}
	}
	if(ba){
		$("#materItem").val(JSON.stringify(data1));
		$("#materItemDel").val(JSON.stringify(datadel1));
		$("#processItem").val(JSON.stringify(data));
		$("#processItemDel").val(JSON.stringify(datadel));
		jQuery.ajaxSettings.async = false;
	  	$("#submitFlag").val(param);
	    $("#inputForm").submit();
	}
	
 }
//选择产品
function selectCPItem(){
	getDG().dialog({
		title:'选择成品',
		width : 800,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/itemmanager.do?ID=' + $("#zd008MaterTypeId").val(),//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[i];
						$("#parentname").val(row.zd002Name);
						$("#parentname1").val(row.zd002Name);
						$("#zd008MaterialsId").val(row.id);
						$("#zd002Name").val(row.zd002Name);
						$("#zd004Model").val(row.zd004Model);
						$("#zd005Size").val(row.zd026Size);
						$("#zd006Unit").val(row.basicStapleUnitDO.id);
						$("#zd006UnitNname").val(row.basicStapleUnitDO.zd002Name);
						$("#parentcode").val(row.zd004Model);
						
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
//选择材质
function selectCZItem(){
	getDG().dialog({
		title:'选择材质',
		width : 400,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/materialsQuality.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[i];
						$("#zd010Quality").val(row.zd002Name);
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
//选择原材料
function selectYCLItem(){
	getDG().dialog({
		title:'选择原材料',
		width : 800,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/itemmanager.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[i];
					materGrid.addRow2({
						zd003MaterialsId : row.id,
						zd001Code : row.zd001Code,
						zd002Name : row.zd002Name,
						zd005Size : row.zd026Size,
						zd004Model : row.zd004Model,
						zd007Unit : row.basicStapleUnitDO.zd002Name,
						zd004MaterTypeId : row.basicMaterialsTypeDO.id
					});	
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
//选择工序
function selectGXItem(){
	getDG().dialog({
		title:'选择工序',
		width : 800,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/process.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[i];
					process.addRow2({
						zd002ProcessId : row.id,
						zd001Code : row.zd001Code,
						zd002Name : row.zd002Name,
						zd001Name : row.basicWorkshopDO.zd002Name
					});	
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}

//关联物料选择工序
function selectMTGXItem(){
	var mrows = materGrid.getCheckedRows();
	if(mrows.length==0){
		$.dialog.alert("请选择需要关联工序的物料！");
   	 	return false;
	}
	getDG().dialog({
		title:'选择工序',
		width : 600,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/process.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[0];
					materGrid.updateCell('zd002ProcessId', row.id, mrows[0]);
					materGrid.updateCell('zd002ProcessName', row.zd002Name, mrows[0]);
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${materbom.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${materbom.bizVersion}"/>
<input type="hidden" id="materItem" name="materItem" value=""/>
<input type="hidden" id="materItemDel" name="materItemDel" value=""/>
<input type="hidden" id="processItem" name="processItem" value=""/>
<input type="hidden" id="processItemDel" name="processItemDel" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         <table class="TableBlock" align="center">
         			<c:if test="${not empty materbom.zd001Code}">
         			<tr>
                    <td>BOM编码:</td>
   		                 <td>
   		                	<input type="text" id="zd001Code" name="zd001Code" style="width: 300px;" readonly="readonly" value="${materbom.zd001Code}" />
   		                 </td>
   		           </tr>
   		           </c:if>
                   <tr>
                    <td>产品类型:</td>
   		                 <td>
   		                	<input type="text" id="zd008MaterTypeName" name="zd008MaterTypeName" value="${materbom.basicMaterialsTypeDO.zd002Name}" style="width: 300px;" readonly="readonly"/>
   		                	<input type="hidden" id="zd008MaterTypeId" name="zd008MaterTypeId"  value="${materbom.zd008MaterTypeId}" />
   		                 </td>
   		                 <td>选择产品:</td>
   		                 <td>
   		               		<input type="text" id="parentname" name="parentname" class="required" value="${materbom.basicMaterialsDO.zd002Name}" readonly="readonly"/>
   		               		<input type="button" value="..." onclick="selectCPItem();"></input>
   		               		<input type="hidden" id="zd008MaterialsId" name="zd008MaterialsId" value="${materbom.zd008MaterialsId}"/>
   		               </td>
   		           </tr>
   		           <tr>
   		           		<td>产品编码:</td>
   		           		<td>
   		           			<input type="text" id="parentcode" name="parentcode" value="${materbom.basicMaterialsDO.zd001Code}" class="required" style="width: 300px;" readonly="readonly"/>
   		           		</td>
   		           		<td>产品名称:</td>
   		           		<td>
   		           			<input type="text" id="parentname1" name="" value="${materbom.basicMaterialsDO.zd002Name}" class="required" style="width: 300px;" readonly="readonly"/>
   		           		</td>
   		           </tr>
   		            <tr>
                    	<td>BOM名称：</td>
   		                 <td>
   		                	<input type="text" id="zd002Name" name="zd002Name" class="required" style="width: 300px;" value="${materbom.zd002Name}"/>
   		                 </td>
   		                 <td>BOM型号：</td>
   		                 <td>
   		                	<input type="text" id="zd004Model" name="zd004Model" style="width: 300px;" value="${materbom.zd004Model}" />
   		                 </td>
   		           </tr>
   		           <tr>
                    	<td>BOM尺寸：</td>
   		                 <td>
   		                	<input type="text" id="zd005Size" name="zd005Size" style="width: 300px;" value="${materbom.zd005Size}" />
   		                 </td>
   		                 <td>BOM单位：</td>
   		                 <td>
   		                 	<input type="text" id="zd006UnitNname" name="zd006UnitNname" class="required" readonly="readonly" value="${materbom.zd006Unit}" />
   		                	<input type="hidden" id="zd006Unit" name="zd006Unit" value="${materbom.zd006Unit}" />
   		                 </td>
   		           </tr>
   		           <tr>
                    	<td>产品系列：</td>
   		                 <td>
   		                   <select id="zd007ProductRange" name="zd007ProductRange" style="width: 300px;">
   		                    <option></option>
   		                    <c:forEach items="${serieslist}" var="series">
   		                    <option value="${series.id}">${series.zd002Name}</option>
   		                    </c:forEach>
   		                   </select>
   		                   <script type="text/javascript">
   		                    $("#zd007ProductRange").val('${materbom.zd007ProductRange}');
   		                   </script>
   		                 </td>
   		                 <td>产品颜色：</td>
   		                 <td>
   		                	<input type="text" id="zd009Color" name="zd009Color" style="width: 300px;" value="${materbom.zd009Color}" />
   		                 </td>
   		           </tr>
   		           <tr>
                    	<td>产品材质：</td>
   		                 <td>
   		                	<input type="text" id="zd010Quality" name="zd010Quality" style="width: 300px;" value="${materbom.zd010Quality}" />
   		                 	<input type="button" onclick="selectCZItem();" value="..."/>
   		                 </td>
   		                 <td>材料比重：</td>
   		                 <td>
   		                	<input type="text" id="zd014Proportion" name="zd014Proportion" style="width: 300px;" value="${materbom.zd014Proportion}" />
   		                 </td>
   		           </tr>
   		           <tr>
                    	<td>材料特征：</td>
   		                 <td>
   		                	<input type="text" id="zd015Features" name="zd015Features" style="width: 300px;" value="${materbom.zd015Features}" />
   		                 </td>
   		                 <td>产品图片：</td>
   		                 <td>
   		                	<input type="text" id="zd013Photo" name="zd013Photo" style="width: 300px;" value="${materbom.zd013Photo}" />
   		                 </td>
   		           </tr>
   		            <tr>
                    <td>助记码</td>
   		                 <td>
   		                	<input type="text" id="bizMnemonic" name="bizMnemonic" style="width: 300px;" value="${materbom.bizMnemonic}" />
   		                 </td>
   		           </tr>
   		           <tr>
   		           	<td>备注</td>
   		           	<td colspan="3"><input id="zd016Remark" style="width: 100%;"  name="zd016Remark" value="${materbom.zd016Remark}" /></td>
   		           </tr>
                </table>               
         </div>
         <div title="物料列表" id="table1"></div>  
         <div title="工序列表" id="table2"></div>  
         <div class="formBar">
	        <ul>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm(1)">保存</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
